var selectedAreaStr = "";
var selectedProductStr = "";
var selectedCompanyStr = "";
$(document).ready(function(){
	var setting = {
			view: {selectedMulti:false},
 			edit: {enable: true,showRemoveBtn: false,showRenameBtn: false},
 			data: {keep: {parent:true,leaf:true},simpleData: {enable: true}},
 			callback: {beforeDrag: beforeDrag,beforeRemove: beforeRemove,beforeRename: beforeRename,onRemove: onRemove}
 	};
	
	var log, className = "dark";
	function beforeDrag(treeId, treeNodes){
		return false;
	}
	function beforeRemove(treeId, treeNode) {
		className = (className === "dark" ? "":"dark");
		showLog("[ "+getTime()+" beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
		return confirm("确认删除 部门 -- " + treeNode.name + " 吗？");
	}
	function onRemove(e, treeId, treeNode) {
		showLog("[ "+getTime()+" onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
	}
	function beforeRename(treeId, treeNode, newName) {
		if (newName.length == 0) {
			alert("部门名称不能为空.");
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			setTimeout(function(){zTree.editName(treeNode)},10);
			return false;
		}else if(newName.length >20){
			alert("部门名称长度不能大于20.");
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			setTimeout(function(){zTree.editName(treeNode)},10);
			return false;
		}
		return true;
	}
	function showLog(str) {
		if (!log) log = $("#log");
		log.append("<li class='"+className+"'>"+str+"</li>");
		if(log.children("li").length > 8) {
			log.get(0).removeChild(log.children("li")[0]);
		}
	}
	function getTime() {
		var now= new Date(),
		h=now.getHours(),
		m=now.getMinutes(),
		s=now.getSeconds(),
		ms=now.getMilliseconds();
		return (h+":"+m+":"+s+ " " +ms);
	}

	var newCount = 1;
	function add(e){
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		isParent = e.data.isParent,
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		if (treeNode) {
			treeNode = zTree.addNodes(treeNode, {id:(100 + newCount), pId:treeNode.id, isParent:isParent, name:"new node" + (newCount++)});
		} else {
			treeNode = zTree.addNodes(null, {id:(100 + newCount), pId:0, isParent:isParent, name:"new node" + (newCount++)});
		}
		if (treeNode) {
			zTree.editName(treeNode[0]);
		} else {
			alert("叶子节点被锁定，无法增加子节点");
		}
	};
	
	
	$("#query").click(function(){		
		$("#dname").attr("disabled","disabled");
		//$("#tempParentId").attr("disabled","disabled");
		$("#allArea").attr("disabled","disabled");
		$("#selectedArea").attr("disabled","disabled");
		$("#allProduct").attr("disabled","disabled");
		$("#selectedProduct").attr("disabled","disabled");
		$("#parentArea").attr("disabled","disabled");
		$("#addBtn").css("display","none");
		$("#updateBtn").css("display","none");		
		$("#parentArea").attr("value","");
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		if (nodes.length == 0) {
			alert("请先选择一个部门");
			return;
		}
		var deptId = treeNode.id;
		 $.ajax({
             type: "POST",
             dataType:"json",
             url:"../../authority/department/getDeptById",
             data:"deptId="+deptId,
             async: false,
             error: function(request) {
                 alert("Connection error");
                 selectedAreaStr = "";
                 selectedProductStr = "";
                 selectedCompanyStr = "";
                 location.reload();
             },
             success: function(data) {
            	 var dept = eval(data);
        		 $("#allProduct").empty();
        		 $("#allArea").empty();
        		 $("#allCompany").empty();
        		 $("#selectedArea").empty();
        		 $("#selectedProduct").empty();
        		 $("#selectedCompany").empty();
        		 var dname = dept.dname;
        		 $("#dname").attr("value",dname);
        		 var parentId = dept.parentId;
        		 var parentName = dept.parentName;
        		 $("#parentId").val(parentId);
        		 $("#tempParentId").val(parentName);
        		 var unUsedAreaList = dept.unUsedAreaList;
/*        		 if(unUsedAreaList!=null && unUsedAreaList!=""){
            		 for ( var int = 0; int < unUsedAreaList.length; int++){
            			 var unUsedArea = unUsedAreaList[int];
            			 var areaID = unUsedArea.areaID;
            			 var name = unUsedArea.name;
            			 $("#allArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
    				} 
        		 }*/
        		 var unUsedProductList = dept.unUsedProductList;
        		 if(unUsedProductList!=null && unUsedProductList!=""){
            		 for ( var int = 0; int < unUsedProductList.length; int++){
            			 var unUsedProduct = unUsedProductList[int];
            			 var id = unUsedProduct.productCode;
            			 var productName = unUsedProduct.productName;
            			 $("#allProduct").prepend("<option value='"+id+"'>"+productName+"</option>");
    				} 
        		 }
        		 var usedAreaList = dept.usedAreaList;
        		 if(usedAreaList!=null && usedAreaList!=""){
               		 for ( var int = 0; int < usedAreaList.length; int++){
            			 var usedArea = usedAreaList[int];
            			 var areaID = usedArea.areaID;
            			 var name = usedArea.name;
            			 $("#selectedArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
    				}	 
        		 }
        		 var usedProductList = dept.usedProductList;
        		 if(usedProductList!=null && usedProductList!=""){
            		 for ( var int = 0; int < usedProductList.length; int++){
            			 var usedProduct = usedProductList[int];
            			 var id = usedProduct.productCode;
            			 var productName = usedProduct.productName;
            			 $("#selectedProduct").prepend("<option value='"+id+"'>"+productName+"</option>");
    				} 
        		 }
        		 
        		 var parentAreaList = dept.parentAreaList;
        		 var areaParentId = dept.areaParentId;
        		 if(parentAreaList!=null && parentAreaList!=""){
               		 for ( var int = 0; int < parentAreaList.length; int++){
            			 var parentArea = parentAreaList[int];
            			 var areaID = parentArea.areaID;
            			 var name = parentArea.name;
            			 if(areaParentId==areaID){
            				 //$("#parentArea").prepend("<option value='"+areaID+"' selected='selected'>"+name+"</option>");
            				 $("#parentArea").prepend("<option value='"+areaID+">"+name+"</option>");
            			 }else{
            				 $("#parentArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
            			 } 
    				}	 
        		 }
        		 
        		 var unUsedCompanyList = dept.unUsedCompanyList;
        		 if(unUsedCompanyList!=null && unUsedCompanyList!=""){
            		 for ( var int = 0; int < unUsedCompanyList.length; int++){
            			 var unUsedCompany = unUsedCompanyList[int];
            			 var id = unUsedCompany.id;
            			 var companyAbb = unUsedCompany.companyAbb;
            			 $("#allCompany").prepend("<option value='"+id+"'>"+companyAbb+"</option>");
    				} 
        		 }
        		 var usedCompanyList = dept.usedCompanyList;
        		 if(usedCompanyList!=null && usedCompanyList!=""){
            		 for ( var int = 0; int < usedCompanyList.length; int++){
            			 var usedCompany = usedCompanyList[int];
            			 var id = usedCompany.id;
            			 var companyAbb = usedCompany.companyAbb;
            			 $("#selectedCompany").prepend("<option value='"+id+"'>"+companyAbb+"</option>");
    				} 
        		 }
        		$("#deptId")[0].value=deptId;
         		$("#greyBackGround").css("display","block");
        		$("#addDept").fadeIn("slow");
             }
         });
	});
	
	function edit(){
		$("#addBtn").css("display","none");
		$("#updateBtn").css("display","inline");
		$("#updateBtn").removeAttr("disabled");
		$("#updateBtn").css("background-color","White");
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		if (nodes.length == 0) {
			alert("请先选择一个部门");
			return;
		}
		var deptId = treeNode.id;
		
		 $.ajax({
             type: "POST",
             dataType:"json",
             url:"../../authority/department/getDeptById",
             data:"deptId="+deptId,
             async: false,
             error: function(request) {
                 alert("Connection error");
                 selectedAreaStr = "";
                 selectedProductStr = "";
                 selectedCompanyStr = "";
                 location.reload();
             },
             success: function(data) {
            	 var dept = eval(data);
        		 $("#allProduct").empty();
        		 $("#allArea").empty();
        		 $("#allCompany").empty();
        		 $("#selectedArea").empty();
        		 $("#selectedProduct").empty(); 
        		 $("#parentArea").empty();
        		 $("#selectedCompany").empty();
        		 var dname = dept.dname;
        		 $("#dname").attr("value",dname);
        		 var parentId = dept.parentId;
        		 var parentName = dept.parentName;
        		 $("#parentId").val(parentId);
        		 $("#tempParentId").val(parentName);
        		 var unUsedAreaList = dept.unUsedAreaList;
/*        		 if(unUsedAreaList!=null && unUsedAreaList!=""){
            		 for ( var int = 0; int < unUsedAreaList.length; int++){
            			 var unUsedArea = unUsedAreaList[int];
            			 var areaID = unUsedArea.areaID;
            			 var name = unUsedArea.name;
            			 $("#allArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
    				} 
        		 }*/
        		 var unUsedProductList = dept.unUsedProductList;
        		 if(unUsedProductList!=null && unUsedProductList!=""){
            		 for ( var int = 0; int < unUsedProductList.length; int++){
            			 var unUsedProduct = unUsedProductList[int];
            			 var id = unUsedProduct.productCode;
            			 var productName = unUsedProduct.productName;
            			 $("#allProduct").prepend("<option value='"+id+"'>"+productName+"</option>");
    				} 
        		 }
        		 var usedAreaList = dept.usedAreaList;
        		 if(usedAreaList!=null && usedAreaList!=""){
               		 for ( var int = 0; int < usedAreaList.length; int++){
            			 var usedArea = usedAreaList[int];
            			 var areaID = usedArea.areaID;
            			 var name = usedArea.name;
            			 $("#selectedArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
    				}	 
        		 }
        		 var usedProductList = dept.usedProductList;
        		 if(usedProductList!=null && usedProductList!=""){
            		 for ( var int = 0; int < usedProductList.length; int++){
            			 var usedProduct = usedProductList[int];
            			 var id = usedProduct.productCode;
            			 var productName = usedProduct.productName;
            			 $("#selectedProduct").prepend("<option value='"+id+"'>"+productName+"</option>");
    				} 
        		 }
        		 
        		 var parentAreaList = dept.parentAreaList;
        		 var areaParentId = dept.areaParentId;
        		 if(parentAreaList!=null && parentAreaList!=""){
               		 for ( var int = 0; int < parentAreaList.length; int++){
            			 var parentArea = parentAreaList[int];
            			 var areaID = parentArea.areaID;
            			 var name = parentArea.name;
            			 $("#parentArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
/*            			 if(areaParentId==areaID){
            				// $("#parentArea").prepend("<option value='"+areaID+"' selected='selected'>"+name+"</option>");
            				 $("#parentArea").prepend("<option value='"+areaID+">"+name+"</option>");
            			 }else{
            				 $("#parentArea").prepend("<option value='"+areaID+"'>"+name+"</option>");
            			 } */
    				}
               		$("#parentArea").prepend("<option value='' selected='selected'>"+'------请选择省份------'+"</option>");
        		 } 
        		 var unUsedCompanyList = dept.unUsedCompanyList;
        		 if(unUsedCompanyList!=null && unUsedCompanyList!=""){
            		 for ( var int = 0; int < unUsedCompanyList.length; int++){
            			 var unUsedCompany = unUsedCompanyList[int];
            			 var id = unUsedCompany.id;
            			 var companyAbb = unUsedCompany.companyAbb;
            			 $("#allCompany").prepend("<option value='"+id+"'>"+companyAbb+"</option>");
    				} 
        		 }
        		 var usedCompanyList = dept.usedCompanyList;
        		 if(usedCompanyList!=null && usedCompanyList!=""){
            		 for ( var int = 0; int < usedCompanyList.length; int++){
            			 var usedCompany = usedCompanyList[int];
            			 var id = usedCompany.id;
            			 var companyAbb = usedCompany.companyAbb;
            			 $("#selectedCompany").prepend("<option value='"+id+"'>"+companyAbb+"</option>");
    				} 
        		 }
        		$("#deptId")[0].value=deptId;
         		$("#greyBackGround").css("display","block");
        		$("#addDept").fadeIn("slow");
             }
         }); 
	};
	
	
	
	
	function remove(e){
		$("#addBtn").css("display","inline");
		$("#updateBtn").css("display","none");
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		if (nodes.length == 0) {
			alert("请先选择一个部门");
			return;
		}
		
		var childrenNodes = treeNode.children;
        if (childrenNodes){
        	alert("您不能删除此部门!");
        	return;
        }
        var isDel = confirm("你确认要删除此部门吗？");
        if(!isDel){
        	return;
        }
        var deptId = treeNode.id;
        if(deptId=="0"){
        	alert("您不能删除新网数码!");
        	return;	
        }else{
   		 $.ajax({
             type: "POST",
             url:"../../authority/department/delDepartment",
             data:"deptId="+deptId,
             async: false,
             error: function(request) {
        		 $("#addDept").css("display","none");
        		 $("#greyBackGround").css("display","none");
          		 $("#areaStr")[0].value="";
          		 $("#productStr")[0].value="";
          		 $("#isCanUseTheDeptName")[0].value="";
          		 $("#dname")[0].value="";
          		 $("#parentId option:nth-child(1)").attr("selected" , "selected");  
                 selectedAreaStr = "";
                 selectedProductStr = "";
                 selectedCompanyStr = "";
                 alert("Connection error");
                 location.reload();
             },
             success: function(data) {
            	 if(data==0){
            		alert("有员工正在使用此部门,不能删除!");
            		return;
            	 }
        		 $("#addDept").css("display","none");
        		 $("#greyBackGround").css("display","none");
          		 $("#areaStr")[0].value="";
          		 $("#productStr")[0].value="";
          		 $("#isCanUseTheDeptName")[0].value="";
          		 $("#dname")[0].value="";
          		 $("#parentId option:nth-child(1)").attr("selected" , "selected");  
                 selectedAreaStr = "";
                 selectedProductStr = "";
                 selectedCompanyStr = "";
                 alert("删除成功!");
            	 location.reload();
             }
         });
        }

	};
	
	
	
	
	function clearChildren(e){
		var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
		nodes = zTree.getSelectedNodes(),
		treeNode = nodes[0];
		if (nodes.length == 0 || !nodes[0].isParent) {
			alert("请先选择一个父节点");
			return;
		}
		zTree.removeChildNodes(treeNode);
	};
		
		
		
	$("#addParent").click(function(){
		$("#tempParentId").attr("value","");
		$("#parentId").attr("value","");
		
		
  		$("#addBtn").attr("disabled","disabled");
  		$("#parentArea").removeAttr("disabled");
 		$("#addBtn").css("background-color", "Gray");
		$("#addBtn").css("display","inline");
		$("#updateBtn").css("display","none");
		$("#greyBackGround").css("display","block");
		$("#addDept").fadeIn("slow");
		//$("#addParent").bind("click", {isParent:true}, add);
	});
		
		
	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	$.fn.zTree.init($("#deptTreeDemo"), setting, zNodes);
	$("#addLeaf").bind("click", {isParent:false}, add);
	$("#edit").bind("click", edit);
	$("#remove").bind("click", remove);
	$("#clearChildren").bind("click", clearChildren);

	var screenwidth, screenheight, mytop, getPosLeft, getPosTop;
	screenwidth = $(window).width();
	screenheight = $(window).height();
	//获取滚动条距顶部的偏移
	mytop = $(document).scrollTop();
	//计算弹出层的left
	getPosLeft = screenwidth / 2 - 550;
	//计算弹出层的top
	getPosTop = screenheight / 2 - 200;
	//css定位弹出层
	$("#deptTree").css({ "left": getPosLeft, "top": getPosTop });
	$("#addDept").css({ "left": getPosLeft, "top": getPosTop });
	//当浏览器窗口大小改变时
	$(window).resize(function(){
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 550;
		getPosTop = screenheight / 2 - 200;
		$("#deptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });
		$("#addDept").css({ "left": getPosLeft, "top": getPosTop + mytop });
	});
	//当拉动滚动条时，弹出层跟着移动
	$(window).scroll(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 550;
		getPosTop = screenheight / 2 - 200;
		$("#deptTree").css({ "left": getPosLeft, "top": getPosTop+ mytop  });	
		$("#addDept").css({ "left": getPosLeft, "top": getPosTop + mytop });
	}); 
	 //获取页面文档的高度
	 var docheight = $(document).height();
	//追加一个层，使背景变灰
	$("body").append("<div id='greyBackGround'></div>");
	$("#greyBackGround").css({ "opacity": "0.1", "height": docheight });
	//登录
	$("#modifyBtn").click(function(){
	$.get("../ashx/login.ashx",{name: $("#txtName").val(),pwd: encodeURIComponent($("#txtPassword").val()),code: $("#txtCode").val()},
		function(data){
			switch (data){
				case "code error":
				alert("验证码错误！");
				break;
				case "success":
				alert("登录成功！");
				break;
				case "false":
				alert("登录失败！");
				break;
				default:
				alert("数据加载失败，请稍后再试！");
				break;
			}
	});
	});
	
	
	 $("#addBtn").click(function(){
		var dname = $("#dname")[0].value;
		if(dname==null || dname==""){
			alert("请输入部门名称");
			return;
		}else if(dname.length>20){
			 $("#checkDeptNameResult").html("部门名称长度不能大于20!");
			 $("#checkDeptNameResult").attr("display","inline");
			 return; 
		 }
		var parentId = $("#parentId").val();
		if(parentId==null || parentId==""){
			alert("请选择部门!");
			return;
		}
		 getSelectedArea();
/*		 if(selectedAreaStr==""){
				alert("请选择区域!");
				return;
		 }*/
		 getSelectedProduct();
	/*	 if(selectedProductStr==""){
				alert("请选择产品!");
				return;
		 }*/
		 getSelectedCompany();
		 $("#areaStr")[0].value=selectedAreaStr;
		 $("#productStr")[0].value=selectedProductStr;
		 $("#companyStr")[0].value=selectedCompanyStr;
		 $.ajax({
             type: "POST",
             url:"../../authority/department/isCanUseTheDeptName",
             data:"dname="+dname+"&isUpdate=false",
             async: false,
             success: function(data) {
            	 $("#isCanUseTheDeptName")[0].value=data;
             	if(data=="false"){
	           		 $("#checkDeptNameResult").html("此部门名已被使用,请重新输入");
	        		 $("#checkDeptNameResult").attr("display","inline");
            		return;
            	}else{
	           		 $("#checkDeptNameResult").html("");
	        		 $("#checkDeptNameResult").attr("display","none");
	        		 $.ajax({
	                     type: "POST",
	                     url:"../../authority/department/addDepartment",
	                     data:$('#deptForm').serialize(),
	                     async: false,
	                     error: function(request) {
	                         selectedAreaStr = "";
	                         selectedProductStr = "";
	                         selectedCompanyStr = "";
	                         alert("Connection error");
	                         location.reload();
	                     },
	                     success: function(data) {
	                  		 $("#areaStr")[0].value="";
	                  		 $("#productStr")[0].value="";
	                  		 $("#isCanUseTheDeptName")[0].value="";
	                  		 $("#dname")[0].value="";
	                  		 $("#parentId option:nth-child(1)").attr("selected" , "selected");  
	                         selectedAreaStr = "";
	                         selectedProductStr = "";
	                         selectedCompanyStr = "";
	                         alert("添加成功!");
	                    	 location.reload();
	                     }
	                 });
            	}
             },
             error: function(request) {
                 alert("Connection error");
             }
         });
	 });
	 
	 $("#updateBtn").click(function(){
 			//$("#updateBtn").removeAttr("disabled");
			//$("#updateBtn").css("background-color","White");
		 	var deptId = $('#deptId').val();
			var dname = $("#dname")[0].value;
			if(dname==null || dname==""){
				alert("请输入部门名称");
				return;
			}else if(dname.length>20){
				 $("#checkDeptNameResult").html("部门名称长度不能大于20!");
				 $("#checkDeptNameResult").attr("display","inline");
				 return; 
			 }
			var parentId = $("#parentId").val();
			if(parentId==null || parentId==""){
				alert("请选择部门!");
				return;
			}
			 getSelectedArea();
/*			 if(selectedAreaStr==""){
					alert("请选择区域!");
					return;
			 }*/
			 getSelectedProduct();
	/*		 if(selectedProductStr==""){
					alert("请选择产品!");
					return;
			 }*/
			 getSelectedCompany();
			 $("#areaStr")[0].value=selectedAreaStr;
			 $("#productStr")[0].value=selectedProductStr;
			 $("#companyStr")[0].value=selectedCompanyStr;
			 $.ajax({
	             type: "POST",
	             url:"../../authority/department/isCanUseTheDeptName",
	             data:"dname="+dname+"&isUpdate=true&id="+deptId,
	             async: false,
	             success: function(data) {
	            	 $("#isCanUseTheDeptName")[0].value=data;
	             	if(data=="false"){
		           		 $("#checkDeptNameResult").html("此部门名已被使用,请重新输入");
		        		 $("#checkDeptNameResult").attr("display","inline");
	            		return;
	            	}else{
		           		 $("#checkDeptNameResult").html("");
		        		 $("#checkDeptNameResult").attr("display","none");
	       			 $.ajax({
	    	             type: "POST",
	    	             url:"../../authority/department/updateDepartment",
	    	             data:$('#deptForm').serialize(),
	    	             async: false,
	    	             error: function(request) {
	    	                 selectedAreaStr = "";
	    	                 selectedProductStr = "";
	    	                 selectedCompanyStr = "";
	    	                 alert("Connection error");
	    	                 location.reload();
	    	             },
	    	             success: function(data) {
	    	        		 $("#addDept").css("display","none");
	    	        		 $("#greyBackGround").css("display","none");
	    	          		 $("#areaStr")[0].value="";
	    	          		 $("#productStr")[0].value="";
	    	          		 $("#isCanUseTheDeptName")[0].value="";
	    	          		 $("#dname")[0].value="";
	    	          		 $("#parentId option:nth-child(1)").attr("selected" , "selected");  
	    	                 selectedAreaStr = "";
	    	                 selectedProductStr = "";
	    	                 selectedCompanyStr = "";
	    	                 alert("更新成功!");
	    	            	 location.reload();
	    	             }
	    	         });	
	            	}
	             },
	             error: function(request) {
	                 alert("Connection error");
	             }
	         });	
	 });
	 
	 $('#dname').blur(function (){
		 var dname = $('#dname').val();
		 var deptId = $('#deptId').val();
		 
		 var isUpdate = false; 
		 var updateBtnDisplay = $("#updateBtn").css("display"); 
		 if(updateBtnDisplay=="inline"){
			 isUpdate=true;
		 }
		 if(dname==null || dname==""){
			 $("#checkDeptNameResult").html("请输入部门名称!");
			 $("#checkDeptNameResult").attr("display","inline");
			// alert("请输入部门名称!");
			 return;
		 }else if(dname.length>20){
			 $("#checkDeptNameResult").html("部门名称长度不能大于20!");
			 $("#checkDeptNameResult").attr("display","inline");
			 return; 
		 }
		 $.ajax({
             type: "POST",
             url:"../../authority/department/isCanUseTheDeptName",
             data:"dname="+dname+"&isUpdate="+isUpdate+"&id="+deptId,
             async: false,
             success: function(data) {
             	if(data=="false"){
             		$("#checkDeptNameResult").html("此部门名已被使用,请重新输入");
             		$("#checkDeptNameResult").attr("display","inline");
             		$("#addBtn").attr("disabled","disabled");
             		$("#addBtn").css("background-color","gray");
             		$("#updateBtn").attr("disabled","disabled");
             		$("#updateBtn").css("background-color","gray");
            		//alert("已经有此部门名称,请重新输入");
            		return;
            	}else{
            		$("#addBtn").removeAttr("disabled");
            		$("#addBtn").css("background-color","White");
            		$("#updateBtn").removeAttr("disabled");
            		$("#updateBtn").css("background-color","White");
            		$("#checkDeptNameResult").html("");
            		$("#checkDeptNameResult").attr("display","none");
            		$("#isCanUseTheDeptName")[0].value=data;	
            	} 
             },
             error: function(request) {
                 alert("Connection error");
             }
         }); 
	 });
	 
	 $("#addBtnCancel").click(function() {
		$("#dname").removeAttr("disabled");
		$("#parentId").removeAttr("disabled");
		$("#allArea").removeAttr("disabled");
		$("#selectedArea").removeAttr("disabled");
		$("#allProduct").removeAttr("disabled");
		$("#parentArea").removeAttr("disabled");
		$("#selectedProduct").removeAttr("disabled");
		 $("#checkDeptNameResult").html("");
		 $("#addDept").css("display","none");
		 $("#greyBackGround").css("display","none");
		 $("#addDept").css("display","none");
		 $("#greyBackGround").css("display","none");
  		 $("#areaStr")[0].value="";
  		 $("#productStr")[0].value="";
  		 $("#isCanUseTheDeptName")[0].value="";
  		 $("#dname")[0].value="";
  		 $("#parentId").attr("value","");
  		 $("#tempParentId").attr("value","");
  		 removeSelectedArea();
  		 removeSelectedProduct();
  		 removeSelectedCompany();
  		 $("#allArea").empty();
  		 $("#parentArea").attr("value","");
  		 
	 });
	 
	 
	 
	 
$("#deptTreeBtnCancel").click(function(){
	$("#deptTreeBackGround").css("display","none");
	$("#deptTree").css("display","none");
});

$("#deptTreeBtnConfirm").click(function(){
	var selectedNodeId="";
	var selectedNodeName="";
	var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
	nodes=treeObj.getSelectedNodes();
	 for ( var int = 0; int < nodes.length; int++){
		 var node = nodes[int];
		 selectedNodeId = node.id;
		 selectedNodeName = node.name;
		 break;
	} 
	if(selectedNodeId==""){
		alert("请选择部门!");
		return;
	}
	$("#parentId").val(selectedNodeId);
	$("#tempParentId").val(selectedNodeName);
	
	$("#deptTreeBackGround").css("display","none");
	$("#deptTree").css("display","none");
});


$("#queryDeptTreeBtnConfirm").click(function(){
	var selectedNodeId="";
	var selectedNodeName="";
	var treeObj=$.fn.zTree.getZTreeObj("deptTreeDemo");
	nodes=treeObj.getSelectedNodes();
	 for ( var int = 0; int < nodes.length; int++){
		 var node = nodes[int];
		 selectedNodeId = node.id;
		 selectedNodeName = node.name;
		 break;
	} 
	if(selectedNodeId==""){
		alert("请选择部门!");
		return;
	}
	$("#parentId").val(selectedNodeId);
	$("#tempParentId").val(selectedNodeName);
	$("#deptTreeBackGround").css("display","none");
	$("#deptTree").css("display","none");
});
		
		
		 
});


function copyAreaToList(from,to){
	//from表示:包含可选择项目的select对象名字 to表示:列出可选择项目的select对象名字 //你可以根据你的具体情况修改 
	fromList = eval('document.forms[0].' + from); 
	
	toList = eval('document.forms[0].' + to); 
	var fromAreaId = fromList.id;
	var toAreaId = toList.id;
	if(fromAreaId=="allArea"){
		if (toList.options.length > 0 && toList.options[0].value == 'temp'){ 
		    toList.options.length = 0; 
		} 
		var sel = false; 
		for (i=0;i<fromList.options.length;i++){
			var current = fromList.options[i]; 
		    if (current.selected){ 
		      sel = true; 
		      if (current.value == 'temp'){ 
		        alert ('你不能选择这个区域!'); 
		        return; 
		      } 
		      txt = current.text; 
		      val = current.value;
		      toList.options[toList.length] = new Option(txt,val); 
		      fromList.options[i] = null; 
		      i--; 
		    } 
		} 
		if (!sel) alert ('请选择区域');
	}else{
		if (toList.options.length > 0 && toList.options[0].value == 'temp'){ 
		    toList.options.length = 0; 
		} 
		var sel = false; 
		for (i=0;i<fromList.options.length;i++){
			var current = fromList.options[i]; 
		    if (current.selected){ 
		      sel = true; 
		      if (current.value == 'temp'){ 
		        alert ('你不能选择这个区域!'); 
		        return; 
		      } 
		      txt = current.text; 
		      val = current.value; 
		      var allAreaOption = $("#allArea option[value='"+val+"']");
			  var hidedAreaOption = $("#hidedArea option[value='"+val+"']");
			  if(hidedAreaOption.length>0 && allAreaOption.length==0){
				  toList.options[toList.length] = new Option(txt,val); 
			  }
		      fromList.options[i] = null; 
		      i--; 
		    } 
		} 
		if (!sel) alert ('请选择区域');
		
	}
 
} 


function copyProductToList(from,to){
	//from表示:包含可选择项目的select对象名字 to表示:列出可选择项目的select对象名字 //你可以根据你的具体情况修改 
	fromList = eval('document.forms[0].' + from); 
	toList = eval('document.forms[0].' + to); 
	if (toList.options.length > 0 && toList.options[0].value == 'temp'){ 
		   toList.options.length = 0; 
	} 
	var sel = false; 
	for (i=0;i<fromList.options.length;i++){
		var current = fromList.options[i]; 
		if (current.selected){ 
		  sel = true; 
		  if (current.value == 'temp') { 
		    alert ('你不能选择这个产品!'); 
		    return; 
		 } 
	     txt = current.text; 
	     val = current.value; 
	     toList.options[toList.length] = new Option(txt,val); 
	     fromList.options[i] = null; 
	     i--; 
	   } 
	} 
	if (!sel) alert ('请选择产品'); 
}	
function copyCompanyToList(from,to){
	//from表示:包含可选择项目的select对象名字 to表示:列出可选择项目的select对象名字 //你可以根据你的具体情况修改 
	fromList = eval('document.forms[0].' + from); 
	toList = eval('document.forms[0].' + to); 
	if (toList.options.length > 0 && toList.options[0].value == 'temp'){ 
		   toList.options.length = 0; 
	} 
	var sel = false; 
	for (i=0;i<fromList.options.length;i++){
		var current = fromList.options[i]; 
		if (current.selected){ 
		  sel = true; 
		  if (current.value == 'temp') { 
		    alert ('你不能选择这个分公司!'); 
		    return; 
		 } 
	     txt = current.text; 
	     val = current.value; 
	     toList.options[toList.length] = new Option(txt,val); 
	     fromList.options[i] = null; 
	     i--; 
	   } 
	} 
	if (!sel) alert ('请选择分公司'); 
}


function getSelectedArea(){ 
	//这是当用户按下提交按钮时，对列出选择的select对象执行全选工作，让递交至的后台程序能取得相关数据 
	  List = document.forms[0].selectedArea; 
	  if (List.length && List.options[0].value == 'temp'){
		  return; 
	  } 
	  for (i=0;i<List.length;i++) { 
		  if(i==0){
			  selectedAreaStr=List.options[i].value;
		  }else{
			  selectedAreaStr= selectedAreaStr+","+List.options[i].value;;
		  }
	  }
}

	
function removeSelectedArea(){
	selectedAreaList = document.forms[0].selectedArea;
	var idArray = new Array();
	for (i=0;i<selectedAreaList.length;i++){
		var id = selectedAreaList.options[i].value;
		var text = selectedAreaList.options[i].text;
		var option = $("#allArea option[value='"+id+"']");
		if(option.length==0){
			$("#allArea").prepend("<option value='"+id+"'>"+text+"</option>");	
		}
		idArray.push(id);
	} 
	for (i=0;i<idArray.length;i++){
		var id = idArray[i];
		$("#selectedArea option[value='"+id+"']").remove();
	}
}

function getSelectedCompany(){ 
	//这是当用户按下提交按钮时，对列出选择的select对象执行全选工作，让递交至的后台程序能取得相关数据 
	  List = document.forms[0].selectedCompany; 
	
	  if (List.length && List.options[0].value == 'temp'){
		  return; 
	  } 
	  for (i=0;i<List.length;i++) { 
		  if(i==0){
			  selectedCompanyStr=List.options[i].value;
		  }else{
			  selectedCompanyStr= selectedCompanyStr+","+List.options[i].value;;
		  }
	  }
}
function removeSelectedCompany(){
	selectedCompanyList = document.forms[0].selectedCompany;
	var idArray = new Array();
	for (i=0;i<selectedCompanyList.length;i++){
		var id = selectedCompanyList.options[i].value;
		var text = selectedCompanyList.options[i].text;
		var option = $("#allArea option[value='"+id+"']");
		if(option.length==0){
			$("#allCompany").prepend("<option value='"+id+"'>"+text+"</option>");	
		}
		idArray.push(id);
	} 
	for (i=0;i<idArray.length;i++){
		var id = idArray[i];
		$("#selectedCompany option[value='"+id+"']").remove();
	}
}
	
function getSelectedProduct(){ 
	//这是当用户按下提交按钮时，对列出选择的select对象执行全选工作，让递交至的后台程序能取得相关数据 
	  List = document.forms[0].selectedProduct; 
	
	  if (List.length && List.options[0].value == 'temp'){
		  return; 
	  } 
	  for (i=0;i<List.length;i++) { 
		  if(i==0){
			  selectedProductStr=List.options[i].value;
		  }else{
			  selectedProductStr= selectedProductStr+","+List.options[i].value;;
		  }
	  }
}
	
	
function removeSelectedProduct(){
	selectedProductList = document.forms[0].selectedProduct;
	var idArray = new Array();
	for (i=0;i<selectedProductList.length;i++){
		var id = selectedProductList.options[i].value;
		var text = selectedProductList.options[i].text;
		var option = $("#allProduct option[value='"+id+"']");
		if(option.length==0){
			$("#allProduct").prepend("<option value='"+id+"'>"+text+"</option>");	
		}
		idArray.push(id);
	} 	
	for (i=0;i<idArray.length;i++){
		var id = idArray[i];
		$("#selectedProduct option[value='"+id+"']").remove();
	}
}
function changeClassB(){
	var parentArea = $("#parentArea").val();
	if(parentArea==null || parentArea==""){
		$("#allArea").empty();
		$("#hidedArea").empty();
		alert("请选择省份！");
		return
	}
	var rootPath = $("#rootPath")[0].value;
	$.ajax({
		type:"POST",
		dataType:"jsond",
		data:{"areaId":parentArea},
		url:rootPath+"/util/changeAreaForClassB",
		success:function(data) {
			var tempHtml="";
			$("#hidedArea").empty();
			data=eval(data);
			for(var i=0;i<data.length;i++) {
				tempHtml = tempHtml+"<option value='"+data[i].areaID+"'>"+data[i].name+"</option>";				
			}
			$("#hidedArea").append(tempHtml);
			$("#allArea").empty();
			//$("#selectedArea").empty();
			var options = $("#selectedArea")[0].options;
			var optionsLength = $("#selectedArea")[0].options.length;
			if(optionsLength==0){
				data=eval(data);
				var html="";
				for(var i=0;i<data.length;i++) {
					html = html+"<option value='"+data[i].areaID+"'>"+data[i].name+"</option>";				
				}
				$("#allArea").append(html);
			}else{
				data=eval(data);
				var html="";
				var allAreaArray = new Array();
				for(var i=0;i<data.length;i++){
					var areaID = data[i].areaID;
					var areaName = data[i].name;
					var isAllArea =true;
					for(var j=0,len=options.length;j<len;j++){
					    var opt = options[j];
					    var value = opt.value;
					    if(areaID==value){
					    	isAllArea=false;
					    	break;
					    }
					}
					if(isAllArea){
						var area = {areaID:areaID,areaName:areaName};
						allAreaArray.push(area);
					}
				}
				html = html+"<option value=''>请选择</option>";		
				for(var i=0;i<allAreaArray.length;i++){
					html = html+"<option value='"+allAreaArray[i].areaID+"'>"+allAreaArray[i].areaName+"</option>";				
				}
				$("#allArea").append(html);
			}
		}
	});
}

function getAllArea(){
	var rootPath = $("#rootPath")[0].value;
	$.ajax({
		type:"POST",
		dataType:"jsond",
		url:rootPath+"/authority/department/getAllArea",
		success:function(data){
			var tempHtml="";
			$("#hidedArea").empty();
			data=eval(data);
			html = html+"<option value=''>请选择</option>";
			for(var i=0;i<data.length;i++) {
				tempHtml = tempHtml+"<option value='"+data[i].areaID+"'>"+data[i].name+"</option>";				
			}
			
			$("#allArea").empty();
			$("#selectedArea").empty();
			var html="";
			for(var i=0;i<data.length;i++) {
				html = html+"<option value='"+data[i].areaID+"'>"+data[i].name+"</option>";				
			}
			$("#selectedArea").append(html);	
			$("#parentArea")[0].value="";
		}
	});
	
}



function queryDeptTree(){
	 $("#queryDeptTreeBtnConfirm").css("display","inline");
	 $("#deptTreeBackGround").css("display","block");
	 $("#deptTree").fadeIn("slow"); 
}


